/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.axiom.ui;

import com.axiom.controller.course_details_controller;
import com.axiom.controller.student_info_controller;
import com.axiom.utility.NullCheckUtility;
import com.axiom.utility.PDFGenerator;
import com.axiom.utility.ScrollUtility;
import com.axiom.vo.course_detail;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.swing.ButtonGroup;
import javax.swing.JFileChooser;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import org.hibernate.ScrollableResults;

/**
 *
 * @author Shuhail
 */
public class ViewStudentAndCourses extends javax.swing.JFrame {

    /**
     * Creates new form ViewStudentAndCourses
     */
    public ViewStudentAndCourses() {
        changeTheLookAndFeel();
        initComponents();
        bg.add(nameRadio);
        bg.add(courseRadio);
        map = ScrollUtility.getInstance().convertToMapStudentCourse(resTmp);
        this.nameRadio.setSelected(true);
         this.courseCombo.setEnabled(false);
        loadCourse();
        loadData("ALL");
        this.deleteButton.setEnabled(false);
        
    }
    
    
    public void changeTheLookAndFeel() {
        
         try {
            
            UIManager.setLookAndFeel(
            UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(StartHere.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(StartHere.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(StartHere.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(StartHere.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
    }
    public void loadDataToString() {
        
        
    }
    public void loadData(String nameMatch) {
       
        if(this.nameRadio.isSelected() && NullCheckUtility.isEmptyOrNull(this.studentNameText.getText())) {
            
            nameMatch="all";
        }
            
        String courseName = this.courseCombo.getSelectedItem().toString();
        System.out.println(courseName +" selected");
        
      //  Object obj = (ScrollableResults)this.resTmp;
 

         DefaultTableModel dft = new DefaultTableModel();
        this.detailsTable.setModel(dft);
        dft.setColumnIdentifiers(new String[]{"First Name","Last Name","Address Line1","Address Line2","Course","Contact"});
       
      
            
           /* System.out.println("here");
            String name =null;
            String course = null;
            String phone = null;
            
            try {
            name = resTmp.get(0).toString();
            
            course = resTmp.get(1).toString();
            
           phone = resTmp.get(2).toString();
             System.out.println(name+"---"+course);
           */
            
         String course;
         String phone;
         String name;
         String lname;
         String addr1;
         String addr2;
         
              
              Iterator it = map.entrySet().iterator();
              while(it.hasNext()) {
                  boolean all = false; 
                  try {
                  Map.Entry<String,String> ent = (Map.Entry<String,String>) it.next();
                  String[] names = ent.getKey().split(":::"); 
                  name = names[1];
                  
                   String[] s = ent.getValue().split(":::");
                   lname = s[0];
                   addr1=s[1];
                   addr2=s[2];
                   course = s[3];
                   phone = s[4];
                   if(!NullCheckUtility.isEmptyOrNull(nameMatch))
                   if(nameMatch.equalsIgnoreCase("ALL")) {
                       dft.addRow(new String[]{name,lname,addr1,addr2,course,phone});
                       all = true;
                   }
                   
                   if(NullCheckUtility.isEmptyOrNull(nameMatch)) {
                   if(course.equals(courseName))
             dft.addRow(new String[]{name,lname,addr1,addr2,course,phone});
                   }
                   else {
                       
                       String smallStudent = nameMatch.toLowerCase();
                       String smallname = name.toLowerCase();
                       String smallLastName = lname.toLowerCase();
                       String address1Small = addr1.toLowerCase();
                       String address2Small = addr2.toLowerCase();
                       if(smallname.contains(smallStudent) || smallLastName.contains(smallStudent) || address1Small.contains(smallStudent) || address1Small.contains(smallStudent) || address2Small.contains(smallStudent)) {
                            dft.addRow(new String[]{name,lname,addr1,addr2,course,phone});
                       }
                       
                   }
                   
                  }
              
        catch(NullPointerException e) {
            
            e.printStackTrace();
        }
            finally {
                 
            }
        
        
              }
    }
    public void loadCourse() {
       
        List list = course_details_controller.getInstance().getAllCourses();
        Iterator it = list.iterator();
        String[] s = new String[list.size()];
        int i=0;
        while(it.hasNext()) {
            
            course_detail detail = (course_detail) it.next();
            s[i] = detail.getName();
            courseCombo.addItem(s[i]);
            i++;
        }
         
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        confirmDialogue = new javax.swing.JDialog();
        jLabel4 = new javax.swing.JLabel();
        yesConfirm = new javax.swing.JButton();
        noConfirm = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jSeparator1 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        detailsTable = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        studentNameText = new javax.swing.JTextField();
        courseCombo = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        nameRadio = new javax.swing.JRadioButton();
        courseRadio = new javax.swing.JRadioButton();
        msg = new javax.swing.JLabel();

        confirmDialogue.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        confirmDialogue.setTitle("Confrim Delete");
        confirmDialogue.setMinimumSize(new java.awt.Dimension(200, 125));
        confirmDialogue.setResizable(false);

        jLabel4.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        jLabel4.setText("Confirm Delete ?");

        yesConfirm.setText("Yes");
        yesConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                yesConfirmActionPerformed(evt);
            }
        });

        noConfirm.setText("No");
        noConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                noConfirmActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout confirmDialogueLayout = new javax.swing.GroupLayout(confirmDialogue.getContentPane());
        confirmDialogue.getContentPane().setLayout(confirmDialogueLayout);
        confirmDialogueLayout.setHorizontalGroup(
            confirmDialogueLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(confirmDialogueLayout.createSequentialGroup()
                .addGroup(confirmDialogueLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(confirmDialogueLayout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(yesConfirm)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(noConfirm))
                    .addGroup(confirmDialogueLayout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(251, Short.MAX_VALUE))
        );
        confirmDialogueLayout.setVerticalGroup(
            confirmDialogueLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(confirmDialogueLayout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jLabel4)
                .addGap(18, 18, 18)
                .addGroup(confirmDialogueLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(yesConfirm)
                    .addComponent(noConfirm))
                .addContainerGap(218, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("View Student and their course");

        jPanel1.setBackground(new java.awt.Color(153, 153, 255));

        detailsTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "First Name", "Last Name", "Address Line1", "Address Line2", "Course", "Contact"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        detailsTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        detailsTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                detailsTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(detailsTable);
        detailsTable.getColumnModel().getColumn(0).setResizable(false);
        detailsTable.getColumnModel().getColumn(1).setResizable(false);
        detailsTable.getColumnModel().getColumn(2).setResizable(false);
        detailsTable.getColumnModel().getColumn(3).setResizable(false);
        detailsTable.getColumnModel().getColumn(5).setResizable(false);

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        jButton1.setText("Save as PDF");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        deleteButton.setText("Delete");
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(okButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addComponent(deleteButton)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okButton)
                    .addComponent(jButton1)
                    .addComponent(deleteButton))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        jLabel2.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N
        jLabel2.setText("Quick Search");

        jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel3.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel3.setText("Quick Search");

        jLabel1.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel1.setText("Course Name  :");

        studentNameText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                studentNameTextKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                studentNameTextKeyReleased(evt);
            }
        });

        courseCombo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                courseComboItemStateChanged(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        nameRadio.setText("Quick Search");
        nameRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nameRadioActionPerformed(evt);
            }
        });
        nameRadio.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                nameRadioKeyReleased(evt);
            }
        });

        courseRadio.setText("By Course");
        courseRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                courseRadioActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(nameRadio)
                    .addComponent(courseRadio))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(nameRadio)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(courseRadio)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(49, 49, 49)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3))
                .addGap(28, 28, 28)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(courseCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(studentNameText, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(studentNameText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(courseCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 555, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(139, 139, 139)
                                .addComponent(jLabel2))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(144, 144, 144)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(msg, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(86, 86, 86)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 318, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(msg, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void courseComboItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_courseComboItemStateChanged
        // TODO add your handling code here:
        System.out.println("State Changed");
        loadData(null);
    }//GEN-LAST:event_courseComboItemStateChanged

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_okButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        
         JFileChooser chooser = new JFileChooser();
         //chooser.setDialogType(JFileChooser.SAVE_DIALOG);
         int result = chooser.showSaveDialog(null);
    switch (result) {
    case JFileChooser.APPROVE_OPTION:
        
      System.out.println("Approve (Open or Save) was clicked");
      String res = chooser.getSelectedFile().toString();
      ScrollableResults resTmp = student_info_controller.getInstance().getStudNameCourseAndContact();
      PDFGenerator.getInstance().generatePDF(res+".pdf", resTmp, 3, "Student Detail");
      //PDFGenerator.getInstance().generatePDFForCourse(res+".pdf", course_details_controller.getInstance().getAllCourses(), 3, "COURSE DETAIL");
      System.out.println(res);
      break;
    case JFileChooser.CANCEL_OPTION:
      System.out.println("Cancel or the close-dialog icon was clicked");
      break;
    case JFileChooser.ERROR_OPTION:
      System.out.println("Error");
      break;
    }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void nameRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nameRadioActionPerformed
        // TODO add your handling code here:
         this.deleteButton.setEnabled(false);
         this.courseCombo.setEnabled(false);
         this.studentNameText.setEnabled(true);
         if(NullCheckUtility.isEmptyOrNull(this.studentNameText.getText()))
         loadData("all");
         else {
             loadData(this.studentNameText.getText());
                     
                     }
    }//GEN-LAST:event_nameRadioActionPerformed

    private void nameRadioKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nameRadioKeyReleased
        // TODO add your handling code here:
        
    }//GEN-LAST:event_nameRadioKeyReleased

    private void courseRadioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_courseRadioActionPerformed
        // TODO add your handling code here:
        this.deleteButton.setEnabled(false);
        this.studentNameText.setEnabled(false);
        this.courseCombo.setEnabled(true);
        loadData(null);
    }//GEN-LAST:event_courseRadioActionPerformed

    private void studentNameTextKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_studentNameTextKeyReleased
        // TODO add your handling code here:
        System.out.println("222");
        if(NullCheckUtility.isEmptyOrNull(this.studentNameText.getText()))
        {
            
            loadData("all");
        }
        loadData(this.studentNameText.getText());
    }//GEN-LAST:event_studentNameTextKeyReleased

    private void studentNameTextKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_studentNameTextKeyPressed
        // TODO add your handling code here:
         System.out.println("22ss2");
    }//GEN-LAST:event_studentNameTextKeyPressed
    
    
    public String getTheIdForDeletion() {
        
        int row = this.detailsTable.getSelectedRow();
        int col = this.detailsTable.getSelectedColumn();
        String data = this.detailsTable.getModel().getValueAt(row, col).toString();
         Iterator it = map.entrySet().iterator();
         boolean isdelete = true;
         String idToDelete = null;
         while(it.hasNext()) {
             
             Map.Entry ent = (Map.Entry) it.next();
             String key = ent.getKey().toString();
             String value = ent.getValue().toString();
             
             if(key.contains(data) || value.contains(data)) {
                 
                 for(int i=0;i<6;i++) {
                     
                     String data1 = this.detailsTable.getModel().getValueAt(row, i).toString();
                     if(!(key.contains(data1) || value.contains(data1))) 
                         isdelete=false;
                     
                 }
                 if(isdelete) {
                     
                     String[] s = key.split(":::");
                     idToDelete = s[0];
                     return idToDelete;
                 }
             }
         
         }
         return idToDelete;
        
    }
    private void detailsTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_detailsTableMouseClicked
        // TODO add your handling code here:
        this.deleteButton.setEnabled(false);
        this.msg.setText("");
        System.out.println(this.detailsTable.getSelectedRow()+"  "+this.detailsTable.getSelectedColumn());
        System.out.println(this.detailsTable.getModel().getValueAt(this.detailsTable.getSelectedRow(), this.detailsTable.getSelectedColumn()));
        String idtodelete = getTheIdForDeletion();
         System.out.println(idtodelete);
        if(!NullCheckUtility.isEmptyOrNull(idtodelete)) {
            
            this.deleteButton.setEnabled(true);
        }
       
       
        //System.out.println(getTheIdForDeletion());
    }//GEN-LAST:event_detailsTableMouseClicked

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        // TODO add your handling code here:
        this.confirmDialogue.setVisible(true);
       
        
    }//GEN-LAST:event_deleteButtonActionPerformed

    private void noConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_noConfirmActionPerformed
        // TODO add your handling code here:
        
        this.confirmDialogue.dispose();
    }//GEN-LAST:event_noConfirmActionPerformed

    private void yesConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_yesConfirmActionPerformed
        // TODO add your handling code here:
        this.deleteConfirm = true;
        this.confirmDialogue.dispose();
         String idToDelete = getTheIdForDeletion();
        if(!NullCheckUtility.isEmptyOrNull(idToDelete) && deleteConfirm) {
            
            int ret = student_info_controller.getInstance().removeStudent(idToDelete);
            if(ret!=0) {
                this.msg.setText("Successfully Deleted the Record");
                resTmp = student_info_controller.getInstance().getStudNameCourseAndContact();
                map = ScrollUtility.getInstance().convertToMapStudentCourse(resTmp);
                loadData("ALL");
                this.deleteButton.setEnabled(false);
                
            }
            System.out.println(ret);
        }
    }//GEN-LAST:event_yesConfirmActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            
             UIManager.setLookAndFeel(
            UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ViewStudentAndCourses.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ViewStudentAndCourses.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ViewStudentAndCourses.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ViewStudentAndCourses.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ViewStudentAndCourses().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JDialog confirmDialogue;
    private javax.swing.JComboBox courseCombo;
    private javax.swing.JRadioButton courseRadio;
    private javax.swing.JButton deleteButton;
    private javax.swing.JTable detailsTable;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel msg;
    private javax.swing.JRadioButton nameRadio;
    private javax.swing.JButton noConfirm;
    private javax.swing.JButton okButton;
    private javax.swing.JTextField studentNameText;
    private javax.swing.JButton yesConfirm;
    // End of variables declaration//GEN-END:variables
String[] data ;
ButtonGroup bg = new ButtonGroup();
ScrollableResults resTmp = student_info_controller.getInstance().getStudNameCourseAndContact();
Map<String,String> map = new HashMap<String, String>();
boolean deleteConfirm = false;
}
